﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class Seller
    {
        private readonly KeRui.DAL.Seller dal = new KeRui.DAL.Seller();

        public int Add(KeRui.Model.Seller model)
        {
            return this.dal.Add(model);
        }

        public int AddGeneralSeller(KeRui.Model.Account modelAccount, int strSellerType)
        {
            return this.dal.AddGeneralSeller(modelAccount, strSellerType);
        }

        public List<KeRui.Model.Seller> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.Seller> list = new List<KeRui.Model.Seller>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.Seller item = new KeRui.Model.Seller();
                    if (dt.Rows[i]["SellerID"].ToString() != "")
                    {
                        item.SellerID = int.Parse(dt.Rows[i]["SellerID"].ToString());
                    }
                    item.SellerName = dt.Rows[i]["SellerName"].ToString();
                    if (dt.Rows[i]["SellerType"].ToString() != "")
                    {
                        item.SellerType = int.Parse(dt.Rows[i]["SellerType"].ToString());
                    }
                    if (dt.Rows[i]["VocationID"].ToString() != "")
                    {
                        item.VocationID = int.Parse(dt.Rows[i]["VocationID"].ToString());
                    }
                    item.VocationName = dt.Rows[i]["VocationName"].ToString();
                    if (dt.Rows[i]["isExtend"].ToString() != "")
                    {
                        if ((dt.Rows[i]["isExtend"].ToString() == "1") || (dt.Rows[i]["isExtend"].ToString().ToLower() == "true"))
                        {
                            item.isExtend = true;
                        }
                        else
                        {
                            item.isExtend = false;
                        }
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.UserName = dt.Rows[i]["UserName"].ToString();
                    if (dt.Rows[i]["AppID"].ToString() != "")
                    {
                        item.AppID = int.Parse(dt.Rows[i]["AppID"].ToString());
                    }
                    item.AppName = dt.Rows[i]["AppName"].ToString();
                    if (dt.Rows[i]["TypeID"].ToString() != "")
                    {
                        item.TypeID = int.Parse(dt.Rows[i]["TypeID"].ToString());
                    }
                    item.TypeName = dt.Rows[i]["TypeName"].ToString();
                    item.SellerSN = dt.Rows[i]["SellerSN"].ToString();
                    item.SellerSNImage = dt.Rows[i]["SellerSNImage"].ToString();
                    item.Province = dt.Rows[i]["Province"].ToString();
                    item.City = dt.Rows[i]["City"].ToString();
                    item.Area = dt.Rows[i]["Area"].ToString();
                    item.Address = dt.Rows[i]["Address"].ToString();
                    item.Zip = dt.Rows[i]["Zip"].ToString();
                    item.Map = dt.Rows[i]["Map"].ToString();
                    item.Tel = dt.Rows[i]["Tel"].ToString();
                    item.Mobile = dt.Rows[i]["Mobile"].ToString();
                    item.Fax = dt.Rows[i]["Fax"].ToString();
                    item.Notice = dt.Rows[i]["Notice"].ToString();
                    item.Images = dt.Rows[i]["Images"].ToString();
                    item.Homepage = dt.Rows[i]["Homepage"].ToString();
                    item.Email = dt.Rows[i]["Email"].ToString();
                    if (dt.Rows[i]["Views"].ToString() != "")
                    {
                        item.Views = int.Parse(dt.Rows[i]["Views"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    item.Guid = dt.Rows[i]["Guid"].ToString();
                    item.SellBrand = dt.Rows[i]["SellBrand"].ToString();
                    item.SellScope = dt.Rows[i]["SellScope"].ToString();
                    if (dt.Rows[i]["MobileCode"].ToString() != "")
                    {
                        item.MobileCode = int.Parse(dt.Rows[i]["MobileCode"].ToString());
                    }
                    item.MapAbcX = dt.Rows[i]["MapAbcX"].ToString();
                    item.MapAbcY = dt.Rows[i]["MapAbcY"].ToString();
                    item.logo = dt.Rows[i]["logo"].ToString();
                    item.Css = dt.Rows[i]["Css"].ToString();
                    item.banner = dt.Rows[i]["banner"].ToString();
                    if (dt.Rows[i]["TemplateFileID"].ToString() != "")
                    {
                        item.TemplateFileID = int.Parse(dt.Rows[i]["TemplateFileID"].ToString());
                    }
                    item.RealName = dt.Rows[i]["RealName"].ToString();
                    item.ViewsStyle = dt.Rows[i]["ViewsStyle"].ToString();
                    if (dt.Rows[i]["Scores"].ToString() != "")
                    {
                        item.Scores = int.Parse(dt.Rows[i]["Scores"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["SellerCost"].ToString() != "")
                    {
                        item.SellerCost = int.Parse(dt.Rows[i]["SellerCost"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int SellerID)
        {
            this.dal.Delete(SellerID);
        }

        public bool Exists(int SellerID)
        {
            return this.dal.Exists(SellerID);
        }

        public DataSet GetCarInfoBySellerIDCache(int sellerID, string strWhere, string strOrder)
        {
            KeRui.BLL.Common common = new KeRui.BLL.Common();
            string cacheKey = string.Concat(new object[] { sellerID, "-", strWhere, "-CarList" });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = common.GetDataTableBySql("select a.*,b.CarLevel,b.CarType,b.VentMete,b.GearBox,b.CarLength,b.CarWidth,b.CarHigh,b.OilConsume,b.ManufacturerPrice,b.SellPrice from CmsContent41 as a ,CmsContent37 as b where b.contentid = a.carid and a.sellerid=" + sellerID + strWhere);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public int GetCarListCount(int sellerID, string strWhere)
        {
            DataSet dataTableBySql = new KeRui.BLL.Common().GetDataTableBySql("select count(*) as c from CmsContent41 as a ,CmsContent37 as b where b.contentid = a.carid and a.sellerid=" + sellerID + strWhere);
            DataTable table = new DataTable();
            if (dataTableBySql.Tables.Count > 0)
            {
                table = dataTableBySql.Tables[0];
            }
            if (table.Rows.Count > 0)
            {
                try
                {
                    return int.Parse(table.Rows[0][0].ToString());
                }
                catch
                {
                    return 0;
                }
            }
            return 0;
        }

        public int GetCount(string strWhere)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("Seller", strWhere);
        }

        public int GetCountByCache(string strWhere)
        {
            string cacheKey = "SellerCount-" + strWhere;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCount(strWhere);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (int) cache;
        }

        public DataTable GetDataForTag(int id)
        {
            string strWhere = "VocationID=" + id;
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return this.dal.GetListForTag(strWhere).Tables[0];
        }

        public List<KeRui.Model.Seller> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("Seller", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Seller> GetList(string stWhere, string strOrder)
        {
            string strWhere = " 1=1 ";
            int pageSize = -1;
            int pageIndex = -1;
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            if (stWhere != null)
            {
                strWhere = strWhere + stWhere;
            }
            DataSet set = common.GetList("Seller", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Seller> GetList(int size, int index, string where, string order)
        {
            string strWhere = where;
            string strOrder = order;
            int pageSize = size;
            int pageIndex = index;
            DataSet set = new KeRui.DAL.Common().GetList("Seller", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Seller> GetListByCache(string strCondition)
        {
            string cacheKey = "SellerList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.Seller>) cache;
        }

        public List<KeRui.Model.Seller> GetListByCache(string tblName, string strWhere, int PageSize, int PageIndex, string strOrder)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string cacheKey = string.Concat(new object[] { tblName, "-", strWhere, "-", PageSize, "-", PageIndex, "List" });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = common.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, "*");
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return this.DataTableToList(((DataSet) cache).Tables[0]);
        }

        public KeRui.Model.Seller GetModel(int SellerID)
        {
            return this.dal.GetModel(SellerID);
        }

        public KeRui.Model.Seller GetModelByCache(int SellerID)
        {
            string cacheKey = "SellerModel-" + SellerID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(SellerID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.Seller) cache;
        }

        public DataSet GetModuleChk(int AppID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SellerModule", " AppID=" + AppID, " ModuleID ", " ModuleID,ModuleName");
        }

        public int GetSellerIDByUserid(int userID)
        {
            return this.dal.GetSellerIDByUserid(userID);
        }

        public DataSet GetSetModel(int SellerID)
        {
            return this.dal.GetSetModel(SellerID);
        }

        public string GetSqlWhere(string SellerName, int SellerType, int TypeID, string CarProvince, string CarCity, string SellBrand, string AppName)
        {
            object obj2;
            string str = " 1=1 ";
            if (SellerName != "")
            {
                str = str + " and SellerName like '%" + Utils.SqlStringFormat(SellerName, 2) + "%'";
            }
            if (SellerType >= 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and SellerType =", SellerType, "" });
            }
            if (TypeID > 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and TypeID =", TypeID, "" });
            }
            if (CarProvince != "")
            {
                str = str + " and Province like '%" + Utils.SqlStringFormat(CarProvince, 2) + "%'";
            }
            if (CarCity != "")
            {
                str = str + " and City like '%" + Utils.SqlStringFormat(CarCity, 2) + "%'";
            }
            if (SellBrand != "")
            {
                str = str + " and SellBrand like '%" + Utils.SqlStringFormat(SellBrand, 2) + "%'";
            }
            if (AppName != "")
            {
                str = str + " and AppName = '" + Utils.SqlStringFormat(AppName, 2) + "'";
            }
            return str;
        }

        public string GetSqlWhere(string SellerID, int FlagLike, string SellerName, int SellerType, string SellerSN, string UserID, string UserName, int VocationID, int AppID, int State, string StartTime, string EndTime)
        {
            object obj2;
            string str = " 1=1 ";
            if (SellerName != "")
            {
                if (FlagLike == 1)
                {
                    str = str + " and SellerName like '%" + Utils.SqlStringFormat(SellerName, 2) + "%'";
                }
                else
                {
                    str = str + " and SellerName ='" + Utils.SqlStringFormat(SellerName) + "'";
                }
            }
            if (SellerID != "")
            {
                if (FlagLike == 1)
                {
                    str = str + " and SellerID like '%" + Utils.SqlStringFormat(SellerID, 2) + "%'";
                }
                else
                {
                    str = str + " and SellerID ='" + Utils.SqlStringFormat(SellerID) + "'";
                }
            }
            if (UserName != "")
            {
                if (FlagLike == 1)
                {
                    str = str + " and UserName like '%" + Utils.SqlStringFormat(UserName, 2) + "%'";
                }
                else
                {
                    str = str + " and UserName ='" + Utils.SqlStringFormat(UserName) + "'";
                }
            }
            if (UserID != "")
            {
                if (FlagLike == 1)
                {
                    str = str + " and UserID like '%" + Utils.SqlStringFormat(UserID, 2) + "%'";
                }
                else
                {
                    str = str + " and UserID ='" + Utils.SqlStringFormat(UserID) + "'";
                }
            }
            if (SellerSN != "")
            {
                if (FlagLike == 1)
                {
                    str = str + " and SellerSN like '%" + Utils.SqlStringFormat(SellerSN, 2) + "%'";
                }
                else
                {
                    str = str + " and SellerSN ='" + Utils.SqlStringFormat(SellerSN) + "'";
                }
            }
            if (SellerType >= 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and SellerType =", SellerType, "" });
            }
            if (State >= 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and State =", State, "" });
            }
            if (VocationID > 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and VocationID =", VocationID, "" });
            }
            if (AppID > 0)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, " and AppID=", AppID, "" });
            }
            if ((StartTime != "") & Utils.IsDateTime(StartTime))
            {
                str = str + " and CreateTime >='" + Utils.SqlStringFormat(StartTime) + "'";
            }
            if ((EndTime != "") & Utils.IsDateTime(EndTime))
            {
                str = str + " and CreateTime <='" + Utils.SqlStringFormat(EndTime) + "'";
            }
            return str;
        }

        public DataSet GetTableByCache(string tblName, string strWhere, int PageSize, int PageIndex, string strOrder)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string cacheKey = string.Concat(new object[] { tblName, "-", strWhere, "-", PageSize, "-", PageIndex, "Table" });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = common.GetList(tblName, PageSize, PageIndex, strWhere, strOrder, "*");
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public void Update(KeRui.Model.Seller model)
        {
            this.dal.Update(model);
        }
    }
}

